import lodash from "lodash";
import {
    fill_cell, get_cell_value,
    get_position_of_para,
    getLastColumnIndexOfSheet,
    set_cell_color,
    set_cells
} from "@/components/js/systemdemo";
import Enum from "@/components/js/enum";
import {get_link_of_extract} from "@/components/js/calculate";

let app = wps.EtApplication();

export function 抽汽3(row) {
    let 高加级数 = JSON.parse(wps.PluginStorage.getItem("高加级数"));
    if (!lodash.includes(高加级数, 3)) { // 如果系统不存在三级高加，则该函数不执行任何操作
        return true
    }
    let data = [
        ["三段抽汽", "三段抽汽压力", "MPa"],
        ["", "三段抽汽温度", "℃"],
        ["", "三段抽汽焓值", "kJ/kg"],
    ];
    let sp = Enum.StandardPoint;
    let leakage = sp.leakage.obj;
    let source = leakage.get_source("三段抽汽"); // 漏至三段抽汽的漏汽的来源，一般为中压前轴封一漏
    let leakage_F = `{leakage.${source}流量}`;
    if (source) {
        data.push(...[
            ["", "三段抽汽压力(漏汽混合后)", "MPa"],
            ["", "三段抽汽温度(漏汽混合后)", "℃"],
            ["", "三段抽汽焓值(漏汽混合后)", "kJ/kg"],
            ["", "漏汽焓值", "kJ/kg"],
            ["", "漏汽流量", "t/h"],
        ])
    }

    let _from_to_ = get_link_of_extract("extract_3");
    let to = _from_to_[1];
    let idx;
    if (to === "heater_1_vapor_in") {
        idx = 1; // 中煤昔阳安平发电厂的一抽进的是二号高加，这种电厂设计就很奇葩
    } else if (to === "heater_2_vapor_in") {
        idx = 2;
    } else if (to === "heater_3_vapor_in") {
        idx = 3;
    }
    // --------------------------- 查询当前高加的进水测点等于哪一集高加的出水测点 -------------------------
    let _from_to_1_ = get_link_of_extract("extract_2");
    let to1 = _from_to_1_[1];
    if (lodash.includes(to1, "3_vapor_in")) {
        to1 = 3;
    } else if (lodash.includes(to1, "2_vapor_in")) {  // 有些电厂的高加编号顺序是反的，1号高加温度最低
        to1 = 2;
    } else if (lodash.includes(to1, "1_vapor_in")) {  // 有些电厂的高加编号顺序是反的，1号高加温度最低
        to1 = 1;
    } else {
        alert("查询二抽蒸汽去向失败，请检查设置")
    }
    // =========================== 查询当前高加的进水测点等于哪一集高加的出水测点 ==========================

    let data2 = [
        ["", idx + "号高加进汽压力", "MPa"],
        ["", idx + "号高加进汽温度", "℃"],
        ["", idx + "号高加进汽焓值", "kJ/kg"],
        ["", idx + "号高加进水压力", "MPa"],
        ["", idx + "号高加进水温度", "℃"],
        ["", idx + "号高加进水焓值", "kJ/kg"],
        ["", idx + "号高加出水压力", "MPa"],
        ["", idx + "号高加出水温度", "℃"],
        ["", idx + "号高加出水焓值", "kJ/kg"],
        ["", idx + "号高加疏水温度", "℃"],
        ["", idx + "号高加疏水焓值", "kJ/kg"], // +15
        ["", idx + "号高加水侧吸热量", "MJ/h"],
        ["", "三段抽汽流量", "t/h"],
        ["", "三段抽汽流量(漏汽混合后)", "t/h"],
        ["", idx + "号高加进汽流量", "t/h"],
        ["", idx + "号高加疏水流量", "t/h"],
        ["", "流经" + idx + "号高加给水流量", "t/h"],  // +20
        ["", "三段抽汽压损", "%"],
        ["", "上端差", "℃"],
        ["", "下端差", "℃"],
        ["", idx + "号高加温升", "℃"],
        ["", "三段抽汽能量", "℃"],
    ];
    data.push(...data2); // 拼接data和data2两个数组，es6语法
    set_cells(row + 2, 1, data);
    set_cell_color(row + 2, 1, 4); // 设置第一个单元格背景色
    app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true); //滚动窗口到更改内容
    let cols = getLastColumnIndexOfSheet("数据整理")
    let row1, row3;
    for (let i = 4; i <= cols; i++) {
        if (source) { // 有漏汽进三抽的情况，默认漏汽至三抽测点之前，即测量的是混合后的蒸汽参数，混合前参数需要计算
            sp.set_para_link("extract_3.P_mixed", sp.get_para_link("extract_3.P"))
            sp.set_para_link("extract_3.T_mixed", sp.get_para_link("extract_3.T"))
            sp.delete_para("extract_3.T")
            fill_cell(row + 2, i, "para", "extract_3.P")
            fill_cell(row + 3, i, "formula", "{extract_3.T}=H_PT({row-1},{row+1})")
            sp.insert_para_link("extract_3.T", `汽机!{Y}${row + 3}`)
            fill_cell(row + 4, i, "formula", "{extract_3.H}=({row+3}*{row+19}-{row+4)*{row+5})/{row+18}")
            fill_cell(row + 5, i, "para", "extract_3.P_mixed")
            fill_cell(row + 6, i, "para", "extract_3.T_mixed")
            fill_cell(row + 7, i, "formula", "{extract_3.H_mixed}=H_PT({row-2},{row-1})")
            if (lodash.includes(source, "中压前轴封")) {
                fill_cell(row + 8, i, "para", "leakage.中压前轴封漏汽焓") // 与enum中name_alias保持一致
            }
            fill_cell(row + 9, i, "para", leakage_F)
            row1 = row + 5;
        } else {
            fill_cell(row + 2, i, "para", "extract_3.P")
            fill_cell(row + 3, i, "formula", "{extract_3.T}")
            fill_cell(row + 7, i, "formula", "{extract_3.H}=H_PT({row-2},{row-1})")
            row1 = row;
        }

        // ------------------------------- 高加进汽 ---------------------------------
        fill_cell(row1 + 5, i, "para", to + ".P");
        fill_cell(row1 + 6, i, "para", to + ".T")
        fill_cell(row1 + 7, i, "formula", "{" + to + ".H}=H_PT({row-2},{row-1})")
        // =============================== 高加进汽 ====================================
        // ----------------------------高加进水-----------------------------------
        let water_in = "heater_" + idx + "_water_in";
        try {
            fill_cell(row1 + 8, i, "para", water_in + ".P")
        } catch (e) {
            fill_cell(row1 + 8, i, "formula",
                "{" + water_in + ".P}={final_feed_water.P}+3*({feed_pump_out.P}-{final_feed_water.P})/" + 高加级数.length);
        }
        // ============================高加进水压力=====================================
        // ---------------------------------高加进水焓值------------------------------------
        try {
            fill_cell(row1 + 9, i, "para", `heater_${idx}_water_in.T`);
        } catch (e) {
            alert("未找到" + idx + "号高加进水温度，但该测点是必要测点，请检查！");
        }
        fill_cell(row1 + 10, i, "formula", `{heater_${idx}_water_in.H}=H_PT({row-2},{row-1})`);
        // ================================ 高加进水焓值 =====================================
        // ------------------------------------ 高加出水 -----------------------------------------
        fill_cell(row1 + 11, i, "para", `heater_${idx}_water_out.P`)
        fill_cell(row1 + 12, i, "para", `heater_${idx}_water_out.T`)
        fill_cell(row1 + 13, i, "formula", "{heater_" + idx + "_water_out.H}=H_PT({row-2},{row-1})")
        // =============================== 高加出水 =================================
        // -------------------------------- 疏水 -------------------------------------
        fill_cell(row1 + 14, i, "para", `heater_${idx}_vapor_out.T`)
        fill_cell(row1 + 15, i, "formula", "{heater_" + idx + "_vapor_out.H}=HW_T({row-1})")
        // ============================疏水=====================================

        // 高加水侧吸热量
        fill_cell(row1 + 16, i, "formula", "{heater_" + idx + ".Q}={row+5}*({row-3}-{row-6})")

        let form1;
        if (source) { // 如果有漏气至高排，则进汽流量=抽汽流量减去漏气流量
            if (lodash.isArray(source)) {
                let temp = "";
                for (let j of source) {
                    temp = temp + `+{leakage.${j}流量}`
                }
                temp = temp.substr(1)
                form1 = `{extract_3.F}={row+1}-(${temp})`
            } else {
                form1 = `{extract_3.F}={row+1}-{leakage.${source}流量}`
            }
            fill_cell(row1 + 17, i, "formula", form1) // 抽汽流量
            fill_cell(row1 + 18, i, "formula", "{extract_3.F_mixed}={row+1}") // 漏汽混合后三抽流量
            row3 = row1 + 1;
        } else {
            fill_cell(row1 + 17, i, `formula`, "{extract_3.F}={row+1}")
            row3 = row1
        }

        let 上级疏水放热量 = `{heater_${to1}_vapor_out.F}*({heater_${to1}_vapor_out.H}-{heater_${idx}_vapor_out.H})`
        let form = `({heater_${idx}.Q}-${上级疏水放热量})/({${to}.H}-{heater_${idx}_vapor_out.H})`
        fill_cell(row3 + 18, i, "formula", `{heater_${idx}_vapor_in.F}=${form}`);// 进汽流量
        // 默认当前级的疏水=当前级的抽汽流量+上一级的疏水流量
        // 疏水流量
        fill_cell(row3 + 19, i, "formula", "{heater_" + idx + "_vapor_out.F}={row-1}+{heater_" + to1 + "_vapor_out.F}")

        // 流经高加的给水流量
        fill_cell(row3 + 20, i, "formula", "{heater_" + idx + ".F}={heater_" + to1 + ".F}")
        if (!Enum.StandardPoint.heater_pre_3["exists"]) { // 如果不存在蒸冷器，则压损直接求值即可
            fill_cell(row3 + 21, i, "formula", `{extract_3.压损}=({extract_3.P}-{${to}.P})/{extract_3.P}*100`)
        } else {
            fill_cell(row3 + 21, i, "formula", "{extract_3.压损}=({extract_3.P}-{row+5})/{extract_3.P}*100")
        }
        fill_cell(row3 + 22, i, "formula", `{heater_${idx}.上端差}=T_P({${to}.P})-{heater_${idx}_water_out.T}`)
        fill_cell(row3 + 23, i, "formula", `{heater_${idx}.下端差}={heater_${idx}_vapor_out.T}-{heater_${idx}_water_in.T}`)
        fill_cell(row3 + 24, i, "formula", `{heater_${idx}.温升}={heater_${idx}_water_out.T}-{heater_${idx}_water_in.T}`)
        fill_cell(row3 + 25, i, "formula", "{extract_3.E}={extract_3.F}*{extract_3.H}")
    }

    // ---------------------------------------- 3号高加蒸冷器 --------------------------------------------
    let row2 = row3 + 24;
    if (!Enum.StandardPoint.heater_pre_3["exists"]) { // 如果蒸冷器不存在，则返回
        return true;
    }
    let data3 = [
        ["蒸冷器", "蒸冷器进汽压力", "MPa"], // +1
        ["", "蒸冷器进汽温度", "℃"],
        ["", "蒸冷器进汽焓值", "kJ/kg"],
        ["", "蒸冷器进水压力", "MPa"],
        ["", "蒸冷器进水温度", "℃"],
        ["", "蒸冷器进水焓值", "kJ/kg"],
        ["", "蒸冷器出水压力", "MPa"],
        ["", "蒸冷器出水温度", "℃"],
        ["", "蒸冷器出水焓值", "kJ/kg"],
        ["", "蒸冷器吸热量", "MJ/h"],
        ["", "蒸冷器进汽流量", "t/h"],
        ["", "流经蒸冷器的给水流量", "t/h"],  //
    ];
    set_cells(row2 + 2, 1, data3);
    set_cell_color(row2 + 2, 1, 4); // 设置第一个单元格背景色
    for (let i = 4; i <= cols; i++) {
        fill_cell(row2 + 2, i, "para", "heater_pre_3_vapor_in.P")
        fill_cell(row2 + 3, i, "para", "heater_pre_3_vapor_in.T")
        fill_cell(row2 + 4, i, "formula", "{heater_pre_3_vapor_in.H}=H_PT({row-2}, {row-1})")
        fill_cell(row2 + 5, i, "para", "heater_pre_3_water_in.P")
        fill_cell(row2 + 6, i, "para", "heater_pre_3_water_in.T")
        fill_cell(row2 + 7, i, "formula", "{heater_pre_3_water_in.H}=H_PT({row-2}, {row-1})")
        let water_out_pos = get_position_of_para("heater_pre_3_water_out.P", i);
        if (water_out_pos) {
            fill_cell(row2 + 8, i, "para", "heater_pre_3_water_out.P");
        } else {
            fill_cell(row2 + 8, i, "formula", "{heater_pre_3_water_out.P}={final_feed_water.P}")
        }
        fill_cell(row2 + 9, i, "para", "heater_pre_3_water_out.T") // 出水温度
        fill_cell(row2 + 10, i, "formula", "{heater_pre_3_water_out.H}=H_PT({row-2}, {row-1})")
        fill_cell(row2 + 11, i, "formula", "{heater_pre_3.Q}={row+1}*({row-7}-{heater_pre_3_vapor_out.H})")
        fill_cell(row2 + 12, i, "formula", "{heater_pre_3_vapor_in.F}={heater_3_vapor_in.F}")
        let form = "{heater_pre_3_water_out.F}={row-2}/({row-3}-{row-6})"
        fill_cell(row2 + 13, i, "formula", form)
    }

    // 添加漏气至三抽测点位置下拉选项
    if (source) {
        let cell = get_cell_value(row + 5, 1, undefined, true)
        set_cell_color(row + 5, 1, 6)
        let current_selected_value = "漏汽至三抽测点之前"
        cell.Validation.Add(3, null, null, "漏汽至三抽测点之前,漏汽至三抽测点之后", 1)
        cell.Value2 = current_selected_value
        cell.Validation.InputMessage = JSON.stringify({"value": current_selected_value, "id": 2}) // 当下拉列表中的值改变时，这里的value记录改变前的值
        cell.Validation.ShowInput = false; // 隐藏掉InputMessage的显示，因为Formula2是只读的，这里使用InputMessage保存单元格的额外信息，不像用户显示该信息
    }
}